package com.atguigu.gulimall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.valid.AddGroup;
import com.atguigu.common.valid.UpdateGroup;
import com.atguigu.common.valid.UpdateShowStatusGroup;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 此注解可以参数校验,但是不能分组校验
 * org.springframework.validation.annotation.Validated 可以
 */
// import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;


/**
 * 品牌
 *
 * @author dengwei
 * @email dengwei@gmail.com
 * @date 2024-04-06 03:17:30
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult bindingResult*/){
        // if (bindingResult.hasErrors()) {
        //     HashMap<String, String> errMsgMap = new HashMap<>();
        //     bindingResult.getFieldErrors().stream().forEach(item ->
        //             errMsgMap.put(item.getField(), item.getDefaultMessage()));
        //     return R.error(400, "提交的数据不合法").put("data", errMsgMap);
        // }

		brandService.save(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand/*, BindingResult bindingResult*/){
        // if (bindingResult.hasErrors()) {
        //     HashMap<String, String> errMsgMap = new HashMap<>();
        //     bindingResult.getFieldErrors().stream().forEach(item ->
        //             errMsgMap.put(item.getField(), item.getDefaultMessage()));
        //     return R.error(400, "提交的数据不合法").put("data", errMsgMap);
        // }


		brandService.updateDetailById(brand);

        return R.ok();
    }

    /**
     * 修改显示状态
     */
    @RequestMapping("/update/showStatus")
    //@RequiresPermissions("product:brand:update")
    public R updateShowStatus(@Validated({UpdateShowStatusGroup.class}) @RequestBody BrandEntity brand/*, BindingResult bindingResult*/){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
